const app = getApp()
// let api = "http://192.168.0.186:6082/beeToken/customer"//明宇
// let api = "http://192.168.0.167:6082/beeToken/customer"//刘炜  
// let api = "http://192.168.0.111:6082/beeToken/customer"//

// let api = "https://bee.xiaodaiyan.com/beeToken/customer"//测试环境
// let apiGov = "https://gov.xiaodaiyan.com/beeToken/government"//测试环境

let api = "https://www.milianmeng.net/beeToken/customer"//正式环境
let apiGov = "https://hkgxq.milianmeng.net/beeToken/government"//正式环境
// 1 api 2 apiGov
function onepost(url, data, cb,type) {
  var token =  wx.getStorageSync('token')
  let urlRequest = api + url
  if(type == 2){
    urlRequest = apiGov + url
  }
  wx.showLoading({
    mask: true,
    title: '加载中',
  })    //加载动画  
  wx.request({
    url: urlRequest,   //域名 （据情况而定）  url为地址
    data: data,  //所需要传的参数
    header: { 
      "Content-Type": "application/x-www-form-urlencoded",   // 处理form表单的请求头
      "X-Access-Token": token
    },
    method: 'POST',    //方式为post
    dataType: 'json',
    responseType: 'text',
    success: function (res) {  // 请求成功后
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else if(res.data.code === -9998){
        wx.clearStorageSync()
      }
      else{
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 3000
        })
        return typeof cb == "function" && cb(res.data)
      }
      wx.hideLoading();
    },
    fail: function (res) {   //请求失败
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
      return typeof cb == "function" && cb(false)
    },
  })
}
//封装接口post    json类型
function twopost(url, data, cb,type) {
  let urlRequest = api + url
  if(!type){
    var token = wx.getStorageSync('token')
  }else if(type == 'no'){
    var token = ''
  }else if(type == 2){
    urlRequest = apiGov + url
  }
  wx.showLoading({
    mask: true,
    title: '加载中',
  })
  wx.request({
    url: urlRequest,
    data: data,
    header: {
      "Content-Type": "application/json;charset=UTF-8",
      "X-Access-Token": token
    },
    method: 'POST',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      wx.hideLoading();
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else{
        setTimeout(()=>{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000
          })
        },100)
        return typeof cb == "function" && cb(res.data)
      }
     
    },
    fail: function (res) {
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
      return typeof cb == "function" && cb(false)
    },
  })
}
//图片专用
function uploadFile(url, data, cb,type) {
  let urlRequest = api + url
  if(type == 2){
    urlRequest = apiGov + url
  }
  // wx.showLoading({
  //   mask: true,
  //   title: '加载中',
  // })
  wx.uploadFile({
    url: urlRequest,
    filePath: data.file,
    name: 'file',
    header: {
      'content-type': 'multipart/form-data',
    }, // 设置请求的 header
    success (res){
      wx.hideLoading();
      let data = JSON.parse(res.data)
      if(data.code == 200){
        return typeof cb == "function" && cb(data)
      }
    }
  })
}
//封装接口get 基本都是json类型的
function get(url, data, cb,type) {
  let urlRequest = api + url
  var token = wx.getStorageSync('token')
  // wx.showLoading({
  //   title: '加载中',
  // })
  if(type == 2){
    urlRequest = apiGov + url
  }
  wx.request({
    url: urlRequest,
    data: data,
    timeout: 10000,
    header: {
      "Content-Type": "application/x-www-form-urlencoded",
      "X-Access-Token": token
    },
    method: 'GET',
    dataType: 'json',
    responseType: 'text',
    success: function (res) {
      wx.hideLoading();
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else if(res.data.code === -9998){
        wx.clearStorageSync()
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 3000
        })
        return typeof cb == "function" && cb(res.data)
        // setTimeout(() => {
        //   wx.switchTab({
        //     url: '/pages/userStore/userStore',
        //   })
        // }, 1500);
      }
      else{
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 3000
        })
        return typeof cb == "function" && cb(res.data)
      }
    },
    fail: function (res) {
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
        return typeof cb == "function" && cb(false)
     },
  })
}
function put(url, data, cb,type) {
  let urlRequest = api + url
  var token = wx.getStorageSync('token')
  if(type == 2){
    urlRequest = apiGov + url
  }
  wx.showLoading({
    mask: true,
    title: '加载中',
  })    //加载动画  
  wx.request({
    url: urlRequest,   //域名 （据情况而定）  url为地址
    data: data,  //所需要传的参数
    header: { 
      "Content-Type": "application/json;charset=UTF-8",
      "X-Access-Token": token
    },
    method: 'put',    //方式为post
    dataType: 'json',
    responseType: 'text',
    success: function (res) {  // 请求成功后
      wx.hideLoading();
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else if(res.data.code === -9998){
        wx.clearStorageSync()
      }
      else{
        setTimeout(()=>{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000
          })
        },100)
        return typeof cb == "function" && cb(res.data)
      }
    },
    fail: function (res) {   //请求失败
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
      return typeof cb == "function" && cb(false)
    },
  })
}

function putTwo(url, data, cb,type) {
  let urlRequest = api + url
  var token = wx.getStorageSync('token')
  // wx.showLoading({
  //   mask: true,
  //   title: '加载中',
  // })    //加载动画  
  if(type == 2){
    urlRequest = apiGov + url
  }
  wx.request({
    url: urlRequest,   //域名 （据情况而定）  url为地址
    data: data,  //所需要传的参数
    header: { 
      "Content-Type": "application/x-www-form-urlencoded",   
      "X-Access-Token": token
    },
    method: 'put',    //方式为post
    dataType: 'json',
    responseType: 'text',
    success: function (res) {  // 请求成功后
      wx.hideLoading();
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else if(res.data.code === -9998){
        wx.clearStorageSync()
      }
      else{
        setTimeout(()=>{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 3000
          })
        },100)
      }
    },
    fail: function (res) {   //请求失败
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
      return typeof cb == "function" && cb(false)
    },
  })
}

function deleteOne(url, data, cb,type) {
  let urlRequest = api + url
  var token = wx.getStorageSync('token')
  if(type == 2){
    urlRequest = apiGov + url
  }
  wx.showLoading({
    mask: true,
    title: '加载中',
  })    //加载动画  
  wx.request({
    url: urlRequest,   //域名 （据情况而定）  url为地址
    data: data,  //所需要传的参数
    header: { 
      "Content-Type": "application/x-www-form-urlencoded",   
      "X-Access-Token": token
    },
    method: 'delete',    //方式为post
    dataType: 'json',
    responseType: 'text',
    success: function (res) {  // 请求成功后
      wx.hideLoading();
      if(res.data.code === 200){
        return typeof cb == "function" && cb(res.data)
      }else if(res.data.code === -9998){
        wx.clearStorageSync()
      }
      else{
        wx.showToast({
          title: res.data.message,
          icon: 'none',
          duration: 3000
        })
      }
    },
    fail: function (res) {   //请求失败
      wx.hideLoading();
      wx.showModal({
        title: '网络错误',
        content: '网络出错，请刷新重试',
        showCancel: false
      })
      return typeof cb == "function" && cb(false)
    },
  })
}
 
//城市检索的首字母
var searchLetter = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z"];
 
// 处理字母列表，插入热门城市
function get_kjList() {
    var searchLetter_new = searchLetter.slice();
    searchLetter_new.unshift('热门城市');
    var arr = [];
    for (var i = 0; i < searchLetter_new.length; i++) {
        arr.push({
            'key': searchLetter_new[i]
        })
    }
    return arr;
}
 

 

//将方法暴露
module.exports = {
  onepost: onepost,
  twopost: twopost,
  get: get,
  put: put,
  putTwo: putTwo,
  uploadFile: uploadFile,
  deleteOne: deleteOne,
  searchLetter: searchLetter,
  get_kjList: get_kjList
}